package com.care.inquiry.server.im.handler;

import com.alibaba.fastjson2.JSON;

import com.care.inquiry.server.im.Command;
import com.care.inquiry.server.im.IMServer;
import com.care.inquiry.server.im.Result;
import io.netty.channel.ChannelHandlerContext;

/**
 * @version: java version 1.8
 * @author: cuiweijie
 * @date: 2024-03-25 20:08
 */
public class ConnectionHandler {

    public static void execute(ChannelHandlerContext ctx, Command command){


        if(IMServer.USERS.containsKey(command.getNickname())){
            ctx.channel().writeAndFlush(Result.fail("该用户已上线,请更换昵称后再试"));
            //从之前分配的EventExecutor注销，并返回ChannelFuture
            ctx.channel().deregister();
            return;
        }

        IMServer.USERS.put(command.getNickname(),ctx.channel());
        ctx.channel().writeAndFlush(Result.success("与服务端连接成功"));
        ctx.channel().writeAndFlush(Result.success(JSON.toJSONString(IMServer.USERS.keySet())));

    }
}
